package org.linlinjava.litemall.wx.service;

import com.sun.javafx.geom.transform.CanTransformVec3d;
import jodd.util.StringUtil;
import net.sf.json.util.JSONUtils;
import org.linlinjava.litemall.db.dao.LitemallCartMapper;
import org.linlinjava.litemall.db.dao.LitemallGoodsMapper;
import org.linlinjava.litemall.db.domain.LitemallCart;
import org.linlinjava.litemall.db.domain.LitemallCartExample;
import org.linlinjava.litemall.db.domain.LitemallGoodsProduct;
import org.linlinjava.litemall.wx.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    LitemallGoodsMapper litemallGoodsMapper;

    @Autowired
    LitemallCartMapper litemallCartMapper;


    String redis_cart_pre="cart";

    @Override
    public void addCart(LitemallCart cart, LitemallGoodsProduct product) {
        HashOperations ops = redisTemplate.opsForHash();
        Map<String,Object> map = new HashMap<>();
        LitemallCart redisCart = (LitemallCart) ops.get(redis_cart_pre + ":" + cart.getUserId(), cart.getGoodsId());
        if(redisCart == null){
            map.put(cart.getGoodsId()+"",cart);
        }else{
            cart.setNumber((short) (redisCart.getNumber()+ cart.getNumber()));
            map.put(cart.getGoodsId()+"",cart);
        }
        ValueOperations<String, String> stringStringValueOperations = stringRedisTemplate.opsForValue();
        String id = stringStringValueOperations.get("id");
        if(StringUtil.isEmpty(id)){
            id = "1";
            stringStringValueOperations.set("id",id);
        }
        Integer autoId = Integer.valueOf(id)+1;
        stringStringValueOperations.set("id",autoId.toString());
        cart.setId(Integer.valueOf(stringStringValueOperations.get("id")));
        ops.put(redis_cart_pre + ":" + cart.getUserId(),cart.getGoodsId(),cart);
    }

    @Override
    public List<LitemallCart> showCart(Integer userId) {
        HashOperations ops = redisTemplate.opsForHash();
        List<LitemallCart> carts = ops.values(redis_cart_pre + ":" + userId);
        return carts;
    }

    @Override
    public List<LitemallCart> updateCart(LitemallCart cart, LitemallGoodsProduct product) {
        return null;
    }

    @Override
    public boolean deleteCart(Integer userId, List<Integer> product) {
        HashOperations ops = redisTemplate.opsForHash();
        List<LitemallCart> carts = ops.values(redis_cart_pre + ":" + userId);
        boolean flag = false;
        for (int i = 0; i < product.size(); i++) {
            Integer productId =  product.get(i);
            for (int j = 0; j < carts.size(); j++) {
                LitemallCart litemallCart = carts.get(j);
                if (litemallCart.getProductId() == productId.longValue()){
                    ops.delete(redis_cart_pre + ":" + userId,litemallCart.getGoodsId());
                    flag = true;
                }
            }
        }
        return flag;
    }

    @Override
    public void updateIsChecked(Integer userId, List<Integer> productIds, boolean isChecked) {
        HashOperations ops = redisTemplate.opsForHash();
        boolean flag = false;
        //获取hash中所有的value值
        List<LitemallCart> values = ops.values(redis_cart_pre + ":" + userId);
        for (int i = 0; i < values.size(); i++) {
            LitemallCart cart = values.get(i);
            if(cart.getUserId().equals(userId)){
                for (int j = 0; j < productIds.size(); j++) {
                    Integer integer = productIds.get(j);
                    if(cart.getProductId().equals(integer)){
                        cart.setChecked(!isChecked);
                        cart.setUpdateTime(new Date());
                        ops.put(redis_cart_pre + ":" + userId, cart.getGoodsId(), cart);
                    }
                }
            }
        }

    }

    @Override
    public void updateCart(Integer userId, LitemallCart cart) {
        HashOperations ops = redisTemplate.opsForHash();
        LitemallCart redisCart = (LitemallCart) ops.get(redis_cart_pre + ":" + userId, cart.getGoodsId());
        redisCart.setNumber(cart.getNumber());
        ops.put(redis_cart_pre + ":" + userId, redisCart.getGoodsId(), redisCart);
    }

    @Override
    public LitemallCart getCartkey(Integer userId, LitemallCart cart) {
        HashOperations ops = redisTemplate.opsForHash();
        LitemallCart redisCart = (LitemallCart) ops.get(redis_cart_pre + ":" + userId, cart.getGoodsId());
        return redisCart;
    }

    @Override
    public LitemallCart litemallCartAdd(LitemallCart cart) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String id = ops.get("id");
        if(StringUtil.isEmpty(id)){
            id = "1";
            ops.set("id",id);
        }
        Integer autoId = Integer.valueOf(id)+1;
        ops.set("id",autoId.toString());
        cart.setId(Integer.valueOf(ops.get("id")));
        valueOperations.set(cart.getUserId()+":"+cart.getId(),cart,60*60*2,TimeUnit.SECONDS);
        return cart;
    }

    @Override
    public LitemallCart getLitemallCart(Integer userId, Integer cartId) {
        ValueOperations ops = redisTemplate.opsForValue();
        LitemallCart cart  = (LitemallCart) ops.get(userId + ":" + cartId) ;
        return cart;
    }

    @Override
    public void deleteCartByUser(Integer userId) {
        HashOperations ops = redisTemplate.opsForHash();
        List<LitemallCart> carts = ops.values(redis_cart_pre + ":" + userId);
        for (LitemallCart cart : carts) {
            if(!cart.getChecked()){
                ops.delete(redis_cart_pre + ":" + userId,cart.getGoodsId());
            }
        }
    }


}
